package com.huajin.codetest.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import com.itextpdf.html2pdf.HtmlConverter;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PdfUtil {

	/**
	 * 行间距
	 */
	private static final float LEADING = -20F;

	/**
	 * 单元格最大字符串长度
	 */
	public static final int BREAK_LINE = 23;

	/**
	 * 换行标志
	 */
	public static final String BREAK_FLAG = "\n";

	private PdfUtil() {}

	/**
	 * PDF合并
	 */
	public static void mergePdf(List<String> sourceFileNameList, String destinationFileName) {
		try (PDDocument destinationFileDocument = new PDDocument()) {
			PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
			pdfMergerUtility.setDestinationFileName(destinationFileName);
			for (String sourceFileName : sourceFileNameList) {
				//-- 合并
				try (PDDocument sourceFileDocument = PDDocument.load(new File(sourceFileName))) {
					pdfMergerUtility.appendDocument(destinationFileDocument, sourceFileDocument);
				}
			}
			destinationFileDocument.save(destinationFileName);
		} catch (Exception e) {
			log.error("", e);
		}
	}

	/**
	 * 重置页码
	 */
	public static void resetPage(String fileName) {
		try (PDDocument pdDocument = PDDocument.load(new File(fileName))) {
			int totalPage = pdDocument.getNumberOfPages();
			for (int i = 0; i < totalPage; i++) {
				PDPage pdPage = pdDocument.getPage(i);
				float x = pdPage.getMediaBox().getWidth() / 2;
				float y = 12;
				//-- 覆盖
				try (PDPageContentStream pdPageContentStream = new PDPageContentStream(pdDocument, pdPage, AppendMode.APPEND, true)) {
					pdPageContentStream.setNonStrokingColor(1F);
					pdPageContentStream.addRect(x - 10, y - 2, 100, 14);
					pdPageContentStream.fill();
				}
				//-- 文本
				try (PDPageContentStream pdPageContentStream = new PDPageContentStream(pdDocument, pdPage, AppendMode.APPEND, true, true)) {
					pdPageContentStream.beginText();
					pdPageContentStream.setFont(PDType1Font.HELVETICA, 8);
					pdPageContentStream.newLineAtOffset(x, y);
					pdPageContentStream.showText((i + 1) + " / " + totalPage);
					pdPageContentStream.endText();
				}
			}
			pdDocument.save(fileName);
		} catch (Exception e) {
			log.error("", e);
		}
	}

	public static void addText(String fileName, String text, float x, float y) {
		try (PDDocument pdDocument = PDDocument.load(new File(fileName))) {
			PDPage pdPage = pdDocument.getPage(pdDocument.getNumberOfPages() - 1);
			//FontType.微软雅黑
			PDFont pdFont = PDType0Font.load(pdDocument, new File(""));
			//-- 文本
			try (PDPageContentStream pdPageContentStream = new PDPageContentStream(pdDocument, pdPage, AppendMode.APPEND, true, true)) {
				pdPageContentStream.beginText();
				pdPageContentStream.setFont(pdFont, 10);
				//-- 拆分
				showMultilineText(pdPageContentStream, text, x, pdPage.getMediaBox().getHeight() - y);
				pdPageContentStream.endText();
			}
			pdDocument.save(fileName);
		} catch (Exception e) {
			log.error("", e);
		}
	}

	private static void showMultilineText(PDPageContentStream pdPageContentStream, String text, float x, float y) throws IOException {
		pdPageContentStream.newLineAtOffset(x, y);
		String[] lines = text.split("\n");
		for (String line : lines) {
			pdPageContentStream.showText(line);
			pdPageContentStream.newLineAtOffset(0, LEADING);
		}
	}

	public static int getLastPageIndex(String fileName) {
		try (PDDocument pdDocument = PDDocument.load(new File(fileName))) {
			return pdDocument.getNumberOfPages() - 1;
		} catch (Exception e) {
			log.error("", e);
		}
		return 0;
	}

	public static void main(String[] args) throws Exception {
		String html = "<h1>Test</h1><p>Hello World</p>";
		String dest = "C:\\Users\\Chinawrr\\Desktop\\test.pdf";
		try (OutputStream pdfStream = new FileOutputStream(dest)) {
			HtmlConverter.convertToPdf(html, pdfStream);
		}
	}

}
